package com.mjfeed.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;

/**
 * @description 收货地址
 * @author zhaominglei
 * @date 2014年10月8日
 */
@Entity
@Table(name="feed_receiver")
public class Receiver extends BaseEntity implements Serializable {
	private static final long serialVersionUID = 9098270639063844835L;
	
	/** 收货地址最大保存数 */
	public static final Integer MAX_RECEIVER_COUNT = 8;
	
	/** 地址 */
	private String address;
	
	/** 地区名称 */
	private String areaName;
	
	/** 收货人 */
	private String consignee;

	/** 是否默认 */
	private Boolean isDefault;

	/** 电话 */
	private String phone;
	
	/** 邮编 */
	private String zipCode;

	/** 地区 */
	private Area area;

	/** 会员 */
	private Member member;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Column(name="area_name")
	public String getAreaName() {
		return areaName;
	}

	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	public String getConsignee() {
		return consignee;
	}

	public void setConsignee(String consignee) {
		this.consignee = consignee;
	}

	@Column(name="is_default", columnDefinition="BIT")
	public Boolean getIsDefault() {
		return isDefault;
	}

	public void setIsDefault(Boolean isDefault) {
		this.isDefault = isDefault;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Column(name="zip_code")
	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="area_id")
	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="member_id",nullable=false, updatable=false)
	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}
	
	/**
	 * 持久化前处理
	 */
	@PrePersist
	public void prePersist() {
		if (getArea() != null) {
			setAreaName(getArea().getFullName());
		}
	}

	/**
	 * 更新前处理
	 */
	@PreUpdate
	public void preUpdate() {
		if (getArea() != null) {
			setAreaName(getArea().getFullName());
		}
	}
}
